package cn.mjnxin.coder.mybatis;

import java.util.HashMap;
import java.util.Map;

import cn.mjnxin.coder.mybatis.util.PropertiesFileUtil;

/**
 * 代码生成类
 * Created by src on 2018/6/25.
 * 放在Dao层下面
 */
public class Generator {
	//Model + ModelExample 存放的路径和包名
	private static String targetProjectDao = ".";
	private static String modelPack = "cn.mjnxin.charge.model.entity";

	//Mapper.java + Mapper.xml 存放路径和包名
	private static String mapperPack = "cn.mjnxin.charge.mybatis.mapper";
//	private static String targetProjectSql = "../../services/dao/src/main/resources/";
	private static String targetProjectSql = "./src/main/resources/";
	private static String sqlmapperPack = "mapper";


	//Repository 存放的路径和包名
//	private static String targetRepository = "../../services/service";
	private static String targetRepository = ".";
	private static String repositoryPack = "cn.mjnxin.charge.service";

	// Api接口Service 的路径和包名
//	private static String targetProjectRpcApi = "../../services/service";
	private static String targetProjectApi = ".";
	private static String apiPack = "cn.mjnxin.charge.service.api";


	// 远程接口ServiceImpl 实现类 的路径和包名
//	private static String targetProjectRpcService = "../../services/service";
	private static String targetProjectApiService = ".";
	private static String serverPack = "cn.mjnxin.charge.service.api.impl";

	private static String targetProjectController = ".";
	private static String controllerPack = "cn.mjnxin.charge.controller";

	//数据库名
	private static String DATABASE = "test_demo";
	//需要生成的数据表前缀
	private static String TABLE_PREFIX = "demo_";
    //不需要生成的数据表前缀
    private static String EXCLUDE_PREFIX = "demo_v_";

	//创建人
	private static String author = "clg";
	private static String JDBC_DRIVER = PropertiesFileUtil.getInstance("generator").get("generator.jdbc.driver");
	private static String JDBC_URL = PropertiesFileUtil.getInstance("generator").get("generator.jdbc.url");
	private static String JDBC_USERNAME = PropertiesFileUtil.getInstance("generator").get("generator.jdbc.username");
	private static String JDBC_PASSWORD = PropertiesFileUtil.getInstance("generator").get("generator.jdbc.password");

	// 需要insert后返回主键的表配置，key:表名,value:主键名
	private static Map<String, String> LAST_INSERT_ID_TABLES = new HashMap<>();
	static {
		LAST_INSERT_ID_TABLES.put("wechat_keyword_activity", "id");
	}

	/**
	 * 自动代码生成
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		MybatisGenerator.generator(
				null, // 是否需要继承某个类，此值是全路径类名
				true, // 是否生成Service层
				false, // 是否生成Controller层
				false, // 是否生成ServiceMock
				targetProjectDao, // 当前执行此方法的模块用于确认base-path 例如 testgenerator/testgenerator-dao
				targetProjectSql, // sqlmap存放 目标项目路径 例如 testgenerator/testgenerator-rpc-service/src/main/resources/
				targetProjectApi, // api接口项目模块  例如 testgenerator/testgenerator-rpc-facade
				targetProjectApiService, // api接口 实现 项目模块  例如 testgenerator/testgenerator-rpc-service
				targetRepository, // Repository 存放在哪个项目
				targetProjectController, // Controller 存放在哪个项目
				modelPack, // model包 例如： com.test.dao.model
				mapperPack, // mapper文件包  com.test.dao.mapper
				repositoryPack, // repository文件包  例如：com.test.dao.repository
				sqlmapperPack, // mapper.xml文件包  mapper
				apiPack, // Api对外接口层的包
				serverPack, // Api实现层
				controllerPack,
				JDBC_DRIVER,
				JDBC_URL,
				JDBC_USERNAME,
				JDBC_PASSWORD,
				DATABASE, // 数据库
				TABLE_PREFIX, // 表前缀，主要是用来匹配需要生成哪些表数据
				EXCLUDE_PREFIX, // 表前缀，主要是用来匹配不需要生成哪些表数据
				LAST_INSERT_ID_TABLES, // 需要insert后返回主键的表配置，key:表名,value:主键名
				author
		);
	}
}